<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="80%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="客户名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="客户地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="客户地址"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="电话"
                :rules="rulesUtils.checkPhone"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contactPerson">
      <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
    </el-form-item>
<!--    <el-form-item label="最后下单时间" prop="lastOrderDate">-->
<!--      <el-input v-model="dataForm.lastOrderDate" placeholder="最后下单时间"></el-input>-->
<!--    </el-form-item>-->


      <el-form-item label="最后下单时间" prop="lastOrderDate">
        <el-date-picker type="date" v-model="dataForm.lastOrderDate" @on-change="_lastOrderDateChange"
                        placeholder="最后下单时间" :editable="false" style="width: 100%"></el-date-picker>
      </el-form-item>

    <el-form-item label="消费（元）" prop="consume">
<!--      <el-input v-model="dataForm.consume" placeholder="消费（元）"-->
<!--                oninput = "value=value.replace(/[^\d.]/g,'')"></el-input>-->
      <el-input-number v-model="dataForm.consume" controls-position="right" style="width: 100%"
                       :precision="2" :step="0.1" :min="0"></el-input-number>
    </el-form-item>
    <el-form-item label="欠款（元）" prop="overdraft">
<!--      <el-input v-model="dataForm.overdraft" placeholder="欠款（元）"-->
<!--                oninput = "value=value.replace(/[^\d.]/g,'')"></el-input>-->
      <el-input-number v-model="dataForm.overdraft" controls-position="right" style="width: 100%"
                       :precision="2" :step="0.1" :min="0"></el-input-number>
    </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import rules, {checkPhoneNumber} from '../../../utils/validate'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          address: '',
          telephone: '',
          contactPerson: '',
          lastOrderDate: '',
          consume: '',
          remark: '',
          overdraft: ''
        },
        dataRule: {
          name: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '客户地址不能为空', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '电话不能为空', trigger: 'blur' },
            { required: true, validator: checkPhoneNumber, trigger: 'blur' }
          ],
          contactPerson: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ]
          // lastOrderDate: [
          //   { required: true, message: '最后下单时间不能为空', trigger: 'blur' }
          // ],
          // consume: [
          //   { required: true, message: '消费（元）不能为空', trigger: 'blur' }
          // ],
          // remark: [
          //   { required: true, message: '备注不能为空', trigger: 'blur' }
          // ],
          // overdraft: [
          //   { required: true, message: '欠款（元）不能为空', trigger: 'blur' }
          // ],
          // status: [
          //   { required: true, message: '状态，0：停用，1：启用不能为空', trigger: 'blur' }
          // ],
        },
        rulesUtils: rules
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sto/client/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.client.name
                this.dataForm.address = data.client.address
                this.dataForm.telephone = data.client.telephone
                this.dataForm.contactPerson = data.client.contactPerson
                this.dataForm.lastOrderDate = data.client.lastOrderDate
                this.dataForm.consume = data.client.consume
                this.dataForm.remark = data.client.remark
                this.dataForm.overdraft = data.client.overdraft
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sto/client/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'telephone': this.dataForm.telephone,
                'contactPerson': this.dataForm.contactPerson,
                'lastOrderDate': this.dataForm.lastOrderDate,
                'consume': this.dataForm.consume,
                'remark': this.dataForm.remark,
                'overdraft': this.dataForm.overdraft
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      _lastOrderDateChange (data) {
        this.dataForm.lastOrderDate = new Date(data)
      }
    }
  }
</script>
